# ifndef __LOGGER_H__
# define __LOGGER_H__
#include <fstream>
#include <iostream>
#include "public.h"
#include "message.h"
//单例模式
/*
饿汉模式
懒汉模式
线程安全的单例模式
*/
class Logger
{
private:
   Logger();

   bool m_LogToTerminal;
   bool m_LogToFile;
   std::fstream m_LogFile;
   LOG_LEVEL m_level;

public:
   static Logger* GetInstance()
   {
       static Logger Logger;
       return &Logger;
   }

   void SetLoToTerminal(bool loToTerminal);
   bool GetLoToTerminal()const;

   void SetLoToLogFile(bool loToLogFile);
   bool GetLoToLogFile()const; 

   void SetLogFile(const std::string& logFileName = "def.log");
   
   void SetLogLevel(LOG_LEVEL level);
   LOG_LEVEL GetLogLevel()const;
   
   //如何使用message类
   void operator+=(const Message& msg)
   {
       if(m_LogToTerminal)
       {
           std::cout<<msg.ToString(true)<<std::endl;
       }

       if(m_LogToFile)
       {
           m_LogFile<<msg.ToString(false)<<std::endl;
       }

       if(msg.ToString().find("FATAL")!=std::string::npos)
       {
           std::cout<<"fatal happened!"<<std::endl;
           exit(-1);
       }
   }
};

#endif 

#define LOG(level) \
    if(level >= Logger::GetInstance()->GetLogLevel()) \
        *Logger::GetInstance() += Message(level,__FILE__,__FUNCTION__,__LINE__) 

#define LOG_INFO LOG(INFO)
#define LOG_ERROR LOG(ERROR)
#define LOG_DEBUG LOG(DEBUG)
//fatal exit
#define LOG_FATAL LOG(FATAL) 